/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.struts.action;

import com.myapp.struts.bo.SysUserBo;
import com.myapp.struts.form.SysUserForm;
import com.myapp.struts.util.NewHibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

/**
 * @document   : LoginAction
   @created on : Jan 17, 2015, 12:37:56 PM
 * @author toint
 */
public class LoginAction extends Action {

    private SessionFactory sessionFactory = NewHibernateUtil.getSessionFactory();
    private String pageForwad = "";
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, 
            HttpServletResponse res) throws Exception {
        SysUserForm frm = (SysUserForm) form;
        String userName = frm.getUserName();
        String password = frm.getPassword();
        String hql = " from SysUserBo u where u.userName = ? and u.password = ? ";
        List<SysUserBo> lstUser = new ArrayList();
        Query q = sessionFactory.getCurrentSession().createQuery(hql);
        if (userName != null && password != null && !userName.trim().equals("") && !password.trim().equals("")) {
            q.setParameter(0, userName);
            q.setParameter(1, password);
            lstUser = q.list();
        }
        if (lstUser != null && lstUser.size() > 0) {
            pageForwad = "home";
        } else {
            pageForwad = "login";
        }
        return mapping.findForward(pageForwad);
    }
    
}
